package com.suyang.syt.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthClass;
import com.suyang.syt.model.OperateLog;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.RegisterDTO;
import com.suyang.syt.service.interfaces.OperateLogService;
import com.suyang.syt.service.interfaces.UserService;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.MyException;
import com.suyang.syt.util.StringUtil;

@Controller
@RequestMapping("/webr")
@AuthClass
public class AdminRegistController {
	@Autowired
	private UserService userService;
	@Autowired
	private OperateLogService operateLogService;

	@RequestMapping(value = "/")
	public String index() {
		return "regist";
	}

	@RequestMapping(value = "/regist", method = RequestMethod.GET)
	public String regist() {
		return "regist";
	}

	@RequestMapping(value = "/regist", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj regist(HttpServletRequest request, RegisterDTO registerDTO) {
		try {
			OperateLog operateLog = new OperateLog();
			operateLog.setAction(registerDTO.getPhone() + "注册了系统");
			operateLog.setIp(request.getRemoteAddr());
			operateLog.setCreateTime(DateFormat.dateToString(new Date()));
			operateLogService.add(operateLog);
			return new AjaxObj(1, "添加成功");
		} catch (MyException e) {
			return new AjaxObj(0, e.getMessage());
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/checkcode", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj registCheckCode(HttpServletRequest request, String username) {
		try {
			AjaxObj ajaxObj = userService.updateRegistCheckCode(username);
			return ajaxObj;
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

}
